﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FONCREAGROINTEGRAL.Models;

namespace FONCREAGROINTEGRAL.Controllers
{
    public class DetalleConceptoLiquidacionController : Controller
    {
        public SistIntFoncreagroEntities db = new SistIntFoncreagroEntities();
        //
        // GET: /DetalleConceptoLiquidacion/

        public ActionResult Index(int id)
        {
            int start = Convert.ToInt32(Request.QueryString["start"]);
            int limit = Convert.ToInt32(Request.QueryString["limit"]);

            var query = db.DETALLECONCEPTOLIQUIDACIONs.Where(x => x.IdPeriodoLaboral == id);

            var result = (from x in query.OrderByDescending(x=>x.IdDetalleConceptoLiquidacion).Skip(start).Take(limit).ToList()
                          select new
                          {
                              x.IdDetalleConceptoLiquidacion,
                              x.Glosa,
                              x.AfectoDeduccion,
                              x.Monto,
                          }).ToList();

            return Json(new { Data = result, total = query.Count() }, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /DetalleConceptoLiquidacion/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /DetalleConceptoLiquidacion/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /DetalleConceptoLiquidacion/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            string strResponse = null;
            bool deduccion = false;
            try
            {
                try
                {
                    deduccion = Convert.ToBoolean(Request.Form["AfectoDeduccion"]);
                }
                catch
                {

                }

                var cDetalleConceptoLiquidacion = new DETALLECONCEPTOLIQUIDACION();
                TryUpdateModel(cDetalleConceptoLiquidacion, collection.ToValueProvider());
                cDetalleConceptoLiquidacion.AfectoDeduccion = deduccion;
                cDetalleConceptoLiquidacion.Pagada = false;
                db.AddToDETALLECONCEPTOLIQUIDACIONs(cDetalleConceptoLiquidacion);
                db.SaveChanges();

                strResponse = "{success: true,msg:'Registro creado correctamente'}";
                return Content(strResponse);
            }
            catch
            {
                strResponse = "{success: false,msg:'Contacte con su Administrador'}";
                return Content(strResponse);
            }
        }
        
        //
        // GET: /DetalleConceptoLiquidacion/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /DetalleConceptoLiquidacion/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /DetalleConceptoLiquidacion/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /DetalleConceptoLiquidacion/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            string strResponse = null;
            try
            {
                // TODO: Add delete logic here
                var reg = db.DETALLECONCEPTOLIQUIDACIONs.Single(x=>x.IdDetalleConceptoLiquidacion == id);
                if (reg.Pagada == false)
                {
                    db.DETALLECONCEPTOLIQUIDACIONs.DeleteObject(reg);
                    db.SaveChanges();
                    strResponse = "OK";
                    return Content(strResponse);
                }
                else
                {
                    strResponse = "ERROR";
                    return Content(strResponse);
                }
            }
            catch
            {
                strResponse = "ERROR";
                return Content(strResponse);
            }
        }
    }
}
